package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.RowFilter.ComparisonType;
import javax.swing.border.EmptyBorder;

import diploma.kisel.modelas.domain.Computer;
import diploma.kisel.modelas.domain.ComputerType;
import diploma.kisel.modelas.domain.DeviceType;
import diploma.kisel.modelas.domain.Program;
import diploma.kisel.modelas.domain.ProgramType;
import diploma.kisel.modelas.service.ComputerServiceImpl;
import diploma.kisel.modelas.service.ProgramServiceImpl;
import diploma.kisel.modelas.service.api.ComputerService;
import diploma.kisel.modelas.service.api.ProgramService;
import diploma.kisel.modelas.validation.Validation;

import javax.swing.JComboBox;

/**
 * This window is used to create, edit and delete programs.
 * 
 * @author Nikolay Kisel
 *
 */
public class ProgramWindow extends JFrame {

	private final int programNumberLength = 10;
	private final int programNameLength = 45;
	private final int programRequirementsLength = 250;
	private final int programVersionLength = 20;

	private ProgramService programService = new ProgramServiceImpl();

	private static ProgramWindow frame;

	private JPanel contentPane;
	private JTextField textFieldNumber;
	private JTextField textFieldName;
	private JTextField textFieldVersion;
	private JTextArea textArea;
	private JButton btnDelete;
	private JComboBox comboBox;

	private Program program;
	private JTextField textFieldCost;


	/**
	 * Create the frame.
	 */
	public ProgramWindow() {
		setBounds(100, 100, 492, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Номер:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		textFieldNumber = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldNumber, 29,
				SpringLayout.EAST, label);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 3,
				SpringLayout.NORTH, textFieldNumber);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldNumber, 10,
				SpringLayout.NORTH, contentPane);
		contentPane.add(textFieldNumber);
		textFieldNumber.setColumns(10);

		JLabel label_1 = new JLabel("Название:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_1);

		textFieldName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldName, 23,
				SpringLayout.SOUTH, textFieldNumber);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 3,
				SpringLayout.NORTH, textFieldName);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldName, 0,
				SpringLayout.WEST, textFieldNumber);
		contentPane.add(textFieldName);
		textFieldName.setColumns(10);

		JLabel label_2 = new JLabel("Версия:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_2);

		textFieldVersion = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldVersion, 23,
				SpringLayout.SOUTH, textFieldName);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 3,
				SpringLayout.NORTH, textFieldVersion);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldVersion, 0,
				SpringLayout.WEST, textFieldNumber);
		contentPane.add(textFieldVersion);
		textFieldVersion.setColumns(10);

		JLabel label_3 = new JLabel("Требования:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 13,
				SpringLayout.NORTH, contentPane);
		contentPane.add(label_3);

		JScrollPane scrollPane = new JScrollPane();
		sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 62,
				SpringLayout.EAST, textFieldName);
		sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, -17,
				SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 16,
				SpringLayout.SOUTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -139,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0,
				SpringLayout.WEST, scrollPane);
		contentPane.add(scrollPane);

		textArea = new JTextArea();
		// contentPane.add(textArea);
		scrollPane.setViewportView(textArea);

		JButton btnSave = new JButton("Save");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnSave, -10,
				SpringLayout.SOUTH, contentPane);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveProgram();
			}
		});
		contentPane.add(btnSave);

		btnDelete = new JButton("Delete");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnDelete, 0,
				SpringLayout.NORTH, btnSave);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnDelete, -6,
				SpringLayout.WEST, btnSave);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteProgram();
			}
		});
		contentPane.add(btnDelete);

		JButton btnCancel = new JButton("Cancel");
		sl_contentPane.putConstraint(SpringLayout.EAST, btnSave, -6,
				SpringLayout.WEST, btnCancel);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCancel, -10,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnCancel, 0,
				SpringLayout.EAST, scrollPane);
		contentPane.add(btnCancel);

		JLabel label_4 = new JLabel("Тип:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_4, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_4);

		comboBox = createComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, comboBox, -3,
				SpringLayout.NORTH, label_4);
		sl_contentPane.putConstraint(SpringLayout.WEST, comboBox, 0,
				SpringLayout.WEST, textFieldNumber);
		contentPane.add(comboBox);

		JLabel label_5 = new JLabel("Стоимость:");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label_5, -93,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_4, 26,
				SpringLayout.SOUTH, label_5);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_5, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_5);

		textFieldCost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCost, -3,
				SpringLayout.NORTH, label_5);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldCost, 0,
				SpringLayout.WEST, textFieldNumber);
		contentPane.add(textFieldCost);
		textFieldCost.setColumns(10);
	}

	public static ProgramWindow getFrame() {
		return frame;
	}

	public static void setFrame(ProgramWindow frame) {
		ProgramWindow.frame = frame;
	}

	public JButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public Program getProgram() {
		return program;
	}

	public void setProgram(Program program) {
		this.program = program;
	}

	public JComboBox createComboBox() {
		Vector<Object> items = new Vector<Object>();
		for (ProgramType programType : ProgramType.values()) {
			items.add(programType.name());
		}
		comboBox = new JComboBox(items);
		// comboBox.setSelectedItem(device.getDeviceType().getId());
		return comboBox;
	}

	public void saveProgram() {
//		Integer id = program.getId();
//		program = new Program();
//		program.setId(id);
		String fieldNumber = textFieldNumber.getText();
		if (Validation.isLengthBetween(fieldNumber, 1, programNumberLength))
			program.setNumber(fieldNumber);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверный номер программы. Номер может содержать до "
							+ programNumberLength + " символов.");
			return;
		}
		String fieldName = textFieldName.getText();
		if (Validation.isLengthBetween(fieldName, 1, programNameLength))
			program.setName(fieldName);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название программы. Название может включать до "
							+ programNameLength + " символов.");
			return;
		}
		String fieldRequirements = textArea.getText();
		if (Validation.isLengthBetween(fieldRequirements, 0,
				programRequirementsLength))
			program.setRequirements(fieldRequirements);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное описание требований. Требования могут включать до "
							+ programRequirementsLength + " символов");
			return;
		}
		String fieldCost = textFieldCost.getText();
		if (Validation.isNumber(fieldCost))
			program.setCost(Integer.valueOf(fieldCost));
		else {
			JOptionPane.showMessageDialog(frame, "Указана неверная цена");
			return;
		}
		String fieldVersion = textFieldVersion.getText();
		if (Validation.isLengthBetween(fieldVersion, 0, programVersionLength))
			program.setVersion(fieldVersion);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное описание версии программы. Версия может включать до "
							+ programVersionLength + " символов.");
		}
		ProgramType programType = ProgramType.valueOf((String) comboBox.getSelectedItem());
		program.setProgramType(programType);
		programService.save(program);
		JOptionPane.showMessageDialog(frame, "Программа сохранена");
		ProgramCatalogWindow.setNewModel(programService.findAll());

	}
	
	public void deleteProgram() {
		int response = JOptionPane.showConfirmDialog(frame, "Удалить данную программу?");
		if (response == JOptionPane.YES_OPTION) {
			programService.remove(program.getId());
			ProgramCatalogWindow.setNewModel(programService.findAll());
			frame.setVisible(false);
		}
			
	}
	
	public void init() {
		if (program!=null) {
			textFieldNumber.setText(program.getNumber());
			textFieldName.setText(program.getName());
			textFieldCost.setText(Integer.toString(program.getCost()));
			textFieldVersion.setText(program.getVersion());
			comboBox.setSelectedItem(program.getProgramType().name());
			System.out.println();
			textArea.setText(program.getRequirements());
		}
	}
	
}
